/**
 *  @Package: com.yidu.drug.action
 *  @author: Administrator
 *  @date: 2018年8月14日 上午9:45:04
 */
package com.yidu.drug.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.model.DrugManagerModel;
import com.yidu.drug.model.DrugRoleModel;
import com.yidu.drug.model.DrugRoleModuleModel;
import com.yidu.drug.model.TreeNode;
import com.yidu.drug.service.DrugModuleService;
import com.yidu.drug.service.DrugRoleService;



/**
 * 类的描述 角色action层
 * @author 祝浪
 * @date 2018年8月14日
 */
@Controller
@RequestMapping("role")
public class DrugRoleAction {
	@Resource
	private DrugRoleService roleService;
	
	@Resource
	private DrugModuleService moduleService;
	
	
	/**
	 * 分页查询所有
	 * @param page 页数
	 * @param limit 行数
	 * @param role 角色对象
	 * @return layui格式的map集合
	 */
	@RequestMapping("/roleFindAll")
	@ResponseBody
	public Map<String,Object> roleFindAll(int page,int limit,DrugRoleModel role){
		
		
		Page pages= new Page();
		//获取页面的页数
		pages.setPage(page);
		//获取页面的行数
		pages.setRows(limit);
		int row=roleService.findRow(role);
		List<DrugRoleModel> list=roleService.findAll(role,pages);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",row);
		map.put("data", list);
		return map;
	}
	@RequestMapping("/roleRemove")
	public String roleRemove(String id){
		
		if(id!=null){
			System.out.println(id);
			roleService.deleteRole(id);
			
			return "ok";
		}
		return "no";
		
	}
	@RequestMapping("/moduleTree")
	@ResponseBody
	public List<TreeNode> moduleTree(String id) throws IOException{
		String pid = "main";//默认查询第一级
		
		if(id!=null&&id!=""){
			List<DrugRoleModuleModel> lists=roleService.findRoleModule(id);
			
			List<TreeNode> list = moduleService.findAllModule(pid,lists);
			id="";
			return list;
		}else{
			//调用findAllModule的方法
			List<TreeNode> list = moduleService.findAllModule(pid);
			id="";
			return list;
		}
		
	}
	@RequestMapping("/addRole")
	public String addRole(HttpSession session,DrugRoleModel vo) throws IOException{
		DrugManagerModel manUser= (DrugManagerModel) session.getAttribute("manager");
		//vo.setRoleOper(manUser.getUsersName());
		vo.setOper("eye");
		roleService.addRole(vo);
		
		return "redirect:../pages/lobby/drugRole.html";
	}
	@RequestMapping("/updateRole")
	public String updateRole(HttpSession session,DrugRoleModel vo){
		
		String manUser=(String) session.getAttribute("manUser");
		//vo.setRoleOper(manUser);
		vo.setOper("eye");
		roleService.updateRole(vo);
		
		String[] ids=vo.getModuleIds().split(",");
		List<DrugRoleModuleModel> list=new ArrayList<DrugRoleModuleModel>();
		for(int i=0; i<ids.length;i++){
			DrugRoleModuleModel erm=new DrugRoleModuleModel();
			erm.setModId(ids[i]);
			erm.setRmId(Tools.getRandomUUID());
			erm.setRoleId(vo.getRoleId());
			list.add(erm);
		}
		roleService.addRoleModule(list);
		return "redirect:../pages/lobby/role.html";
	}
}
